Skip to content

Conversation

@Gaurang-5
Copy link
Contributor

Description

Fixes an infinite loop in the JBIG2 decoder when processing PDFs that use symbol dictionary context retention and reuse features.

Problem

The JBIG2 decoder was missing the implementation of bitmap coding context retention and reuse as specified in ISO/IEC 14492 section 7.4.2.2, steps 3 and 7. When a PDF used these features (indicated by the bitmapCodingContextUsed and bitmapCodingContextRetained flags), the decoder would enter an infinite loop because it was creating new arithmetic coding contexts instead of reusing existing ones.

Solution

This PR implements:

  1. Context retention: When bitmapCodingContextRetained is set, the decoder now stores the arithmetic coding contexts after decoding a symbol dictionary, indexed by segment number.

  2. Context reuse: When bitmapCodingContextUsed is set, the decoder retrieves and reuses contexts from the last referred-to symbol dictionary segment. The implementation includes validation to ensure that the coding parameters (huffman, refinement, template, refinementTemplate) match between the original and reusing segments.

  3. Helper methods: Added getRetainedBitmapContexts() and setRetainedBitmapContexts() to manage the context storage.

The fix properly assigns decodingContext.contextCache during reuse instead of leaving it undefined, which eliminates the infinite loop.

Testing

  • Added regression test PDF bitmap-symbol-context-reuse.pdf that previously caused an infinite loop
  • Updated test_manifest.json with test entry for issue 20461
  • Verified the PDF now renders correctly without hanging

Fixes #20461

…ention and add regression test

Implements JBIG2 spec section 7.4.2.2 steps 3 and 7 for bitmap coding
context retention and reuse. When bitmapCodingContextUsed is set, the
decoder now reuses arithmetic coding contexts from the last referred-to
symbol dictionary instead of creating new ones. When bitmapCodingContextRetained
is set, contexts are saved for future reuse.

Fixes mozilla#20461
@Gaurang-5
Copy link
Contributor Author

@timvandermeij

@timvandermeij
Copy link
Contributor

/botio-linux preview

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_preview from @timvandermeij received. Current queue size: 0

Live output at: http://54.241.84.105:8877/070ff3f62b453a4/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Success

Full output at http://54.241.84.105:8877/070ff3f62b453a4/output.txt

Total script time: 0.99 mins

Published

@timvandermeij
Copy link
Contributor

/botio test

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_test from @timvandermeij received. Current queue size: 0

Live output at: http://54.241.84.105:8877/cff5b2f9e2dff4f/output.txt

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Received

Command cmd_test from @timvandermeij received. Current queue size: 1

Live output at: http://54.193.163.58:8877/caaf5fe16482bc1/output.txt

@timvandermeij timvandermeij changed the title Fix infinite loop in JBIG2 decoder with symbol dictionary context ret… Fix infinite loop in JBIG2 decoder with symbol dictionary context retention Dec 28, 2025
@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/cff5b2f9e2dff4f/output.txt

Total script time: 41.14 mins

  • Unit tests: Passed
  • Integration Tests: Passed
  • Regression tests: FAILED
  different ref/snapshot: 1

Image differences available at: http://54.241.84.105:8877/cff5b2f9e2dff4f/reftest-analyzer.html#web=eq.log

@moz-tools-bot
Copy link
Collaborator

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/caaf5fe16482bc1/output.txt

Total script time: 79.46 mins

  • Unit tests: FAILED
  • Integration Tests: FAILED
  • Regression tests: FAILED
  different ref/snapshot: 1

Image differences available at: http://54.193.163.58:8877/caaf5fe16482bc1/reftest-analyzer.html#web=eq.log

}

// Reuse the arithmetic coding contexts
decodingContext.contextCache = retainedContexts.contextCache;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I open the PDF file from this commit with the preview build at http://54.241.84.105:8877/070ff3f62b453a4/web/viewer.html there is no infinite loop anymore, but I also don't see any image rendering, and the console shows this:

Warning: Unable to decode image "img_p0_1": "TypeError: setting getter-only property "contextCache"". pdf.worker.mjs:357:13
Warning: Dependent image isn't ready yet

That seems to relate to this line, so it looks like this may not actually work as expected?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: pdf.js busy-loops forever on JBIG2 image that uses the symbol dictionary context retain/reuse feature

3 participants